package com.wmh.baseservice.operationaid.mapper;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.operationaid.entity.KeyWord;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 谢骏
 * @description 针对表【operation_aid_key_word(运营助手-关键词数据)】的数据库操作Mapper
 * @createDate 2023-05-26 16:49:26
 * @Entity generator.domain.KeyWord
 */

public interface KeyWordMapper extends BaseMapper<KeyWord> {

    /**
     * 获取可选的周期列表
     * @param categoryName 名称
     * @return 数据周期列表
     */
    default List<String> selectCycleList(String categoryName) {
        return selectList(new LambdaQueryWrapperX<KeyWord>()
                .eq(StringUtils.isNotBlank(categoryName), KeyWord::getCategoryName, categoryName)
                .groupBy(KeyWord::getCycle)
                .orderByDesc(KeyWord::getCreateTime)
                .select(KeyWord::getCycle))
                .stream().map(KeyWord::getCycle).collect(Collectors.toList());
    }

    /**
     * 获取可选的创建时间列表 按 "天" 进行分组
     *
     * @return 创建时间列表
     */
    default List<String> selectCreateTimeList() {
        return selectList(new QueryWrapper<KeyWord>()
                .select("date(create_time) as createTime")
                .groupBy("createTime")
                .orderByDesc("createTime"))
                .stream().map(item -> LocalDateTimeUtil.formatNormal(item.getCreateTime().toLocalDate()))
                .collect(Collectors.toList());
    }

    /**
     * 获取品类名称
     * @param cycle 数据周期
     * @return
     */
    default List<String> selectCategoryNameList(String cycle) {
        return selectList(new LambdaQueryWrapperX<KeyWord>()
                .eq(StringUtils.isNotBlank(cycle), KeyWord::getCycle, cycle)
                .groupBy(KeyWord::getCategoryName)
                .orderByDesc(KeyWord::getCreateTime)
                .select(KeyWord::getCategoryName))
                .stream().map(KeyWord::getCategoryName).collect(Collectors.toList());
    }
}